﻿using System;

namespace PickGold.Base.OCR.QRCode.Render
{
	public class FixedCodeSize : ISizeCalculation
	{
		private int _CodeWidth;
		private int _QuietZoneModules = 1;

		/// <summary>
		/// QrCodeWidth is pixel size of QrCode you would like to print out. 
		/// It have to be greater than QrCode's matrix width(include quiet zone).
		/// QrCode matrix width is between 25 ~ 182(version 1 ~ 40).
		/// </summary>
		public int CodeWidth
		{
			get
			{
				return this._CodeWidth;
			}
			set
			{
				if (value <= 0)
					throw new ArgumentOutOfRangeException();

				this._CodeWidth = value;
			}
		}

		/// <summary>
		/// Number of quietZone modules
		/// </summary>
		public int QuietZoneModules
		{
			get
			{
				return this._QuietZoneModules;
			}
			set
			{
				this._QuietZoneModules = value;
			}
		}

		/// <summary>
		/// FixedCodeSize is strategy for rendering QrCode at fixed Size. 
		/// </summary>
		/// <param name="codeWidth">Fixed size for QrCode pixel width. 
		/// Pixel width have to be bigger than QrCode's matrix width(include quiet zone)
		/// QrCode matrix width is between 25 ~ 182(version 1 ~ 40).</param>
		public FixedCodeSize(int codeWidth, int quietZone)
		{
			this._CodeWidth = codeWidth;
			this._QuietZoneModules = quietZone;
		}

		/// <summary>
		/// Interface function that use by Rendering class.
		/// </summary>
		/// <param name="matrixWidth">QrCode matrix width</param>
		/// <returns>Module pixel size and QrCode pixel width</returns>
		public DrawingSize GetSize(int matrixWidth)
		{
			var i = this._QuietZoneModules;
			i = i * 2 + matrixWidth;
			i = this._CodeWidth / i;
			return new DrawingSize(i, this._CodeWidth, this._QuietZoneModules);
		}
	}
}
